// 替换枚举为静态常量类
import 'dart:collection';

class MessageType {
  static const String loginMessage = 'loginMessage';
  static const String signalMessage = 'signalMessage';
  static const String textMessage = 'textMessage';
}

class SignalType {
  static const String offer = 'offer';
  static const String answer = 'answer';
  static const String candidate = 'candidate';
}


// 抽象类 Message
abstract class Message {
  late final String msgType;
  Message(){
    msgType = getMsgType();
  }
  String getMsgType();

  // 将 JSON 转化为指定对象
  static Message fromJson(Map<String, dynamic> json) {
    String type = json['msgType'] ?? '';

    switch (type) {
      case MessageType.loginMessage:
        return LoginMessage.fromJson(json);
      case MessageType.signalMessage:
        return SignalMessage.fromJson(json);
      case MessageType.textMessage:
        return TextMessage.fromJson(json);
      default:
        throw ArgumentError('Unknown message type: $type');
    }
  }
}

// LoginMessage 类
class LoginMessage extends Message {
  final String name;
  final int port;
  late final String key;

  LoginMessage(this.name, this.port) {
    key = _md5();
  }

  @override
  String getMsgType() {
    return MessageType.loginMessage;
  }

  String _md5() {
    // 处理 md5 生成 key
    return name + port.toString();
  }

  // 静态方法用于从 JSON 创建实例
  static LoginMessage fromJson(Map<String, dynamic> json) {
    return LoginMessage(
      json['name'] ?? '',
      json['port'] ?? 0,
    );
  }
}

// SignalMessage 类
class SignalMessage<T> extends Message {
  final String type; // SignalType 改为 String 类型
  final T data; // data可以是offer，answer，候选人

  SignalMessage(this.data, this.type);

  @override
  String getMsgType() {
    return MessageType.signalMessage;
  }

  static SignalMessage<T> fromJson<T>(Map<String, dynamic> json) {
    return SignalMessage<T>(
      json['data'] as T,
      json['type'] ?? '',
    );
  }
}

// TextMessage 类
class TextMessage extends Message {
  final String name;
  final String key;
  final String text;

  TextMessage(this.name, this.key, this.text);

  @override
  String getMsgType() {
    return MessageType.textMessage;
  }

  static TextMessage fromJson(Map<String, dynamic> json) {
    return TextMessage(
      json['name'] ?? '',
      json['key'] ?? '',
      json['text'] ?? '',
    );
  }
}

// 示例使用
void main() {
  Map<String, dynamic> loginJson = {
    'msgType': 'loginMessage',
    'name': 'Alice',
    'port': 8080,
  };

  Message message = Message.fromJson(loginJson);
  final lm = message as LoginMessage;
  print(lm.port); // 输出：LoginMessage 实例
}

// 登录的客户端信息
class Client{

}

class MessageHandler{
  static sendMessage(dynamic text){

  }
}

final Map<String,Client> clientMap = HashMap();
void test(json){

  String type = json['msgType'] ?? '';
  switch (type) {
    case MessageType.loginMessage:
      loginHandler(LoginMessage.fromJson(json));
    case MessageType.signalMessage:
      signalHandler(SignalMessage.fromJson(json));
    case MessageType.textMessage:
      textHandler(TextMessage.fromJson(json));
    default:
      throw ArgumentError('Unknown message type: $type');
  }

}

void loginHandler(LoginMessage login){
  if(!clientMap.containsKey(login.key)){
    final client = Client();
    clientMap[login.key] = client;
    MessageHandler.sendMessage(client);
  }else{
    MessageHandler.sendMessage("登录失败，key已存在");
  }
}

void signalHandler(SignalMessage signal){
  switch(signal.type){
    case SignalType.offer:
      signal.data;
      break;
    case SignalType.answer:
      break;
    case SignalType.candidate:
      break;
    default:
      throw ArgumentError("未知参数");
  }
}

void textHandler(TextMessage text){

}
